package com.lmp.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.AbsoluteLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UiParams {
	public float scalex;
	public float scaley;
	Activity activity;

	public int Screen_width;
	public int Screen_height;
	
	public Map<String, Integer> LiftMap = new HashMap<String, Integer>();
	
	int GUI_LCD_XMAX=0;
	int GUI_LCD_YMAX=0; 

	REGION G_REG_DIR = new REGION(); 
	REGION G_REG_FLR = new REGION(); 
	REGION G_REG_TIME = new REGION(); 
	REGION G_REG_DAY = new REGION(); 
	REGION G_REG_WEEK = new REGION(); 
	REGION G_REG_ALARM = new REGION(); 
	REGION G_REG_BKGND = new REGION(); 
	REGION G_REG_LOGO = new REGION();
	REGION G_REG_OTHER = new REGION(); 
	REGION G_REG_VIDEO = new REGION(); 
	REGION G_REG_TXT = new REGION(); 
	 
	int G_LFTTXT_txt_color=0;
	int G_LFTTXT_font_id=2; 
	
	public UiParams(Activity activity){
		this.activity = activity;
	}

	public int dip2px_x(int dpValue) {  
        return (int)(dpValue * scalex + 0.5f);
    }
	
	public int dip2px_y(int dpValue) {  
        return (int)(dpValue * scaley + 0.5f);
    }
		 
	public void initLandLayoutParams() {
		
		Log.d("LIFT","initLandLayoutParams......................");

		GUI_LCD_XMAX = ConvertArea("LCD_XMAX", true);
		GUI_LCD_YMAX = ConvertArea("LCD_YMAX", false);
		
		G_REG_DIR.s_x = ConvertArea("REG_DIR.SX", true);
		G_REG_DIR.s_y = ConvertArea("REG_DIR.SY", false);
		G_REG_DIR.x_len = ConvertArea("REG_DIR.XLEN", true);
		G_REG_DIR.y_len = ConvertArea("REG_DIR.YLEN", false);
		
		G_REG_FLR.s_x = ConvertArea("REG_FLR.SX", true);
		G_REG_FLR.s_y = ConvertArea("REG_FLR.SY", false);
		G_REG_FLR.x_len = ConvertArea("REG_FLR.XLEN", true);
		G_REG_FLR.y_len = ConvertArea("REG_FLR.YLEN", false);
		
		G_REG_TIME.s_x = ConvertArea("REG_TIME.SX", true);
		G_REG_TIME.s_y = ConvertArea("REG_TIME.SY", false);
		G_REG_TIME.x_len = ConvertArea("REG_TIME.XLEN", true);
		G_REG_TIME.y_len = ConvertArea("REG_TIME.YLEN", false);
		
		G_REG_DAY.s_x = ConvertArea("REG_DAY.SX", true);
		G_REG_DAY.s_y = ConvertArea("REG_DAY.SY", false);
		G_REG_DAY.x_len = ConvertArea("REG_DAY.XLEN", true);
		G_REG_DAY.y_len = ConvertArea("REG_DAY.YLEN", false);
		
		G_REG_WEEK.s_x = ConvertArea("REG_WEEK.SX", true);
		G_REG_WEEK.s_y = ConvertArea("REG_WEEK.SY", false);
		G_REG_WEEK.x_len = ConvertArea("REG_WEEK.XLEN", true);
		G_REG_WEEK.y_len = ConvertArea("REG_WEEK.YLEN", false);
		
		G_REG_ALARM.s_x = ConvertArea("REG_ALARM.SX", true);
		G_REG_ALARM.s_y = ConvertArea("REG_ALARM.SY", false);
		G_REG_ALARM.x_len = ConvertArea("REG_ALARM.XLEN", true);
		G_REG_ALARM.y_len = ConvertArea("REG_ALARM.YLEN", false);
		
		G_REG_BKGND.s_x = ConvertArea("REG_BKGND.SX", true);
		G_REG_BKGND.s_y = ConvertArea("REG_BKGND.SY", false);
		G_REG_BKGND.x_len = ConvertArea("REG_BKGND.XLEN", true);
		G_REG_BKGND.y_len = ConvertArea("REG_BKGND.YLEN", false);
		
		G_REG_LOGO.s_x = ConvertArea("REG_LOGO.SX", true);
		G_REG_LOGO.s_y = ConvertArea("REG_LOGO.SY", false);
		G_REG_LOGO.x_len = ConvertArea("REG_LOGO.XLEN", true);
		G_REG_LOGO.y_len = ConvertArea("REG_LOGO.YLEN", false);
		
		G_REG_OTHER.s_x = ConvertArea("REG_OTHER.SX", true);
		G_REG_OTHER.s_y = ConvertArea("REG_OTHER.SY", false);
		G_REG_OTHER.x_len = ConvertArea("REG_OTHER.XLEN", true);
		G_REG_OTHER.y_len = ConvertArea("REG_OTHER.YLEN", false);
		
		G_REG_VIDEO.s_x = ConvertArea("REG_VIDEO.SX", true);
		G_REG_VIDEO.s_y = ConvertArea("REG_VIDEO.SY", false);
		G_REG_VIDEO.x_len = ConvertArea("REG_VIDEO.XLEN", true);
		G_REG_VIDEO.y_len = ConvertArea("REG_VIDEO.YLEN", false);

		G_REG_TXT.s_x = ConvertArea("REG_TXT.SX", true);
		G_REG_TXT.s_y = ConvertArea("REG_TXT.SY", false);
		G_REG_TXT.x_len = ConvertArea("REG_TXT.XLEN", true);
		G_REG_TXT.y_len = ConvertArea("REG_TXT.YLEN", false);	

	}
	 
	public void getDisplayMetrics() throws Exception {
		 	initArea();
		 
	  		DisplayMetrics dm = new DisplayMetrics();
	  		dm = activity.getApplicationContext().getResources().getDisplayMetrics();
	  		Screen_width = dm.widthPixels;
	  		Screen_height = dm.heightPixels;
//	  		Screen_width = 960;
//	  		Screen_height = 540;
	  		
	  		scalex =  (float)Screen_width/(float)(LiftMap.get("LCD_XMAX"));
	  		scaley =  (float)Screen_height/(float)(LiftMap.get("LCD_YMAX"));
	  		Log.d("LIFT","getDisplayMetrics......................");
	  		Log.d("LIFT", Screen_width + "," + Screen_height + "," + scalex + "," + scaley);
	}
	 
	public void initArea() throws Exception{
			Log.d("LIFT","initArea......................");
			//String csvPath = Environment.getExternalStorageDirectory()+ "/LIFT_0C.csv";
			String csvPath = "LIFT_0C.csv";
			CsvParser parser=new CsvParser(csvPath);
			
			Object[] arr=parser.getParsedArray();
			
			for(Object obj:arr){          
			    List<String> ls=(List<String>)obj;           
			    if(ls.size()>=2)
			    {
			    	Log.d("LIFT", ls.get(0).toString() + " " + ls.get(1).toString()); 
			    	LiftMap.put(ls.get(0).toString(), Integer.parseInt(ls.get(1).toString()));
			    }
			}			
	 }
	 
	 public int ConvertArea(String str, boolean x) {
		 if(LiftMap.containsKey(str))
		 {	 
			 if(x) {
				 Log.d("LIFT", str + "," + LiftMap.get(str) + " " + dip2px_x(LiftMap.get(str)));
				 return dip2px_x(LiftMap.get(str));
			 }else {
				 Log.d("LIFT", str + "," + LiftMap.get(str) + " " + dip2px_y(LiftMap.get(str)));
				 return dip2px_y(LiftMap.get(str));
			 }			 			 
		 }
		 else
			 return 0;		 
	 } 
}
